package com.kurumi.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfOutline;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFUtil {

	public static ByteArrayOutputStream getPDFStream(List<Map<String,Object>> imageFiles,String imageBasicPath) throws MalformedURLException, IOException, DocumentException{
    	ByteArrayOutputStream bos = new ByteArrayOutputStream();
    	Document document = new Document(PageSize.A4);
    	document.setMargins(0, 0, 0, 0);
    	PdfWriter writer = PdfWriter.getInstance(document, bos);

		document.open();
		// Code 2

		Map<String, String> pageTypes = new HashMap<String, String>();
		List<String> pageTypeCodes = new ArrayList<String>();
		for (Map<String, Object> imageFile : imageFiles) {
			/*Image image = Image.getInstance(BarCodeUtil.generate("123456789"));*/
			String imagePath = imageBasicPath + GuidUtil.getLocalPath((String)imageFile.get("file_hash"))+"\\"
 					+(String)imageFile.get("file_name")+"."+(String)imageFile.get("file_type");
        	String pageTypeCode = (String)(String)imageFile.get("mr_page_type_code");
        	String pageTypeName = (String)(String)imageFile.get("page_type_name");
        	if(pageTypeCode == null){
        		pageTypeCode = "-01";
        		pageTypeName = "未编页";
        	}

			Image image = Image.getInstance(imagePath);
	        // 获取操作的页面
	        // 根据域的大小缩放图片
	        //image.scaleToFit(signRect.getWidth(), signRect.getHeight());
	        // 添加图片
			float imageHeight=image.getScaledHeight();
			float imageWidth=image.getScaledWidth();
			int i=0;
			while(imageHeight>PageSize.A4.getHeight()||imageWidth>PageSize.A4.getWidth()){
				image.scalePercent(100-i);
				i++;
				imageHeight=image.getScaledHeight();
				imageWidth=image.getScaledWidth();
			}
			/*image.scalePercent(100);*/
			image.setAlignment(Image.ALIGN_CENTER);
			document.newPage();
			if(!pageTypes.containsKey(pageTypeCode)){
				document.add(new Chunk(pageTypeName).setLocalDestination(pageTypeCode));
				pageTypes.put(pageTypeCode, pageTypeName);
				pageTypeCodes.add(pageTypeCode);
			}

			document.add(image);

		}

		PdfContentByte cb = writer.getDirectContent();
		PdfOutline root = cb.getRootOutline();
		for (String pageTypeCode : pageTypeCodes) {
			 @SuppressWarnings("unused")
		     PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage(pageTypeCode, false),pageTypes.get(pageTypeCode));

		}
		// Code 3



	 	document.close();
	 	return bos;

	}


	public static synchronized void createPdf(List<Map<String,Object>> imageFiles,String imageBasicPath,String newPDFPath) throws DocumentException, IOException{
    	Document document = new Document();
    	document.setPageSize(PageSize.A4);
    	FileOutputStream out = null;

		try {
			File newFile = new File(newPDFPath);
			if(!newFile.exists()){
				File parentFile = new File(newFile.getParent());
				if(!parentFile.exists()){
					parentFile.mkdirs();
				}
			}else{
				return;
			}
			out = new FileOutputStream(newPDFPath);
			PdfWriter writer = PdfWriter.getInstance(document, out);
			// 打开文档
			document.open();
			// 读取一个图片
			Map<String, String> pageTypes = new HashMap<String, String>();
			List<String> pageTypeCodes = new ArrayList<String>();
			for (Map<String, Object> sourceFile : imageFiles) {
				String fileType = (String)sourceFile.get("file_type");
				if(fileType.equalsIgnoreCase("PNG")|| fileType.equalsIgnoreCase("JPG")||fileType.equalsIgnoreCase("JPEG") || fileType.equalsIgnoreCase("TIF") || fileType.equalsIgnoreCase("TIFF")){

					String imagePath = imageBasicPath + GuidUtil.getLocalPath((String)sourceFile.get("file_hash"))+"\\"
		 					+(String)sourceFile.get("file_name");
		        	String pageTypeCode = (String)(String)sourceFile.get("mr_page_type_code");
		        	String pageTypeName = (String)(String)sourceFile.get("page_type_name");
		        	if(pageTypeCode == null){
		        		pageTypeCode = "-01";
		        		pageTypeName = "未编页";
		        	}

					Image image = Image.getInstance(imagePath);
			        // 获取操作的页面
			        // 根据域的大小缩放图片
			        //image.scaleToFit(signRect.getWidth(), signRect.getHeight());
			        // 添加图片
					float imageHeight=image.getScaledHeight();
					float imageWidth=image.getScaledWidth();
					int i=0;
					while(imageHeight>PageSize.A4.getHeight()||imageWidth>PageSize.A4.getWidth()){
						image.scalePercent(100-i);
						i++;
						imageHeight=image.getScaledHeight();
						imageWidth=image.getScaledWidth();
					}
					/*image.scalePercent(100);*/
					image.setAlignment(Image.TOP);
					image.setAbsolutePosition(0, 0);
					document.newPage();
					if(!pageTypes.containsKey(pageTypeCode)){
						document.add(new Chunk(pageTypeName,new Font(FontFamily.UNDEFINED, 18, 0, BaseColor.WHITE)).setLocalDestination(pageTypeCode));


						pageTypes.put(pageTypeCode, pageTypeName);
						pageTypeCodes.add(pageTypeCode);
					}
					document.add(image);
				}
				/*Image image = Image.getInstance(BarCodeUtil.generate("123456789"));*/


			}
			PdfContentByte cb = writer.getDirectContent();
			PdfOutline root = cb.getRootOutline();
			for (String pageTypeCode : pageTypeCodes) {
				 @SuppressWarnings("unused")
			     PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage(pageTypeCode, false),pageTypes.get(pageTypeCode));

			}


		} catch (DocumentException de) {
			de.printStackTrace();
			throw de;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw ioe;
		}finally{
			try {
				if(out!= null){
					out.flush();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			try{
				if(document != null){
					document.close();
				}
			}catch (Exception e) {
				// TODO: handle exception
			}
			try {
				if(out!= null){
					out.close();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
    }


	public static synchronized void createPdf(List<Map<String,Object>> imageFiles,String imageBasicPath,String pageIndexPDFPath,String newPDFPath) throws DocumentException, IOException{
    	Document document = new Document();
    	document.setPageSize(PageSize.A4);
    	FileOutputStream out = null;

		try {
			File newFile = new File(newPDFPath);
			if(!newFile.exists()){
				File parentFile = new File(newFile.getParent());
				if(!parentFile.exists()){
					parentFile.mkdirs();
				}
			}else{
				return;
			}
			out = new FileOutputStream(newPDFPath);
			PdfWriter writer = PdfWriter.getInstance(document, out);
			// 打开文档
			document.open();
			if(pageIndexPDFPath != null){
				File pageIndexPDFFile = new File(pageIndexPDFPath);
				if(pageIndexPDFFile.exists()){
					PdfReader pdfReader = new PdfReader(pageIndexPDFPath);
					// 打开文档
					document.open();
					PdfContentByte cb = writer.getDirectContent();
					int pageOfCurrentReaderPDF = 0;
					while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
		    	        document.newPage();
		    	        pageOfCurrentReaderPDF++;
		                PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
		                cb.addTemplate(page, 0, 0);

		    	    }
				}
			}



			// 读取一个图片
			/*Map<String, String> pageTypes = new HashMap<String, String>();
			List<String> pageTypeCodes = new ArrayList<String>();*/
			for (Map<String, Object> sourceFile : imageFiles) {
				String fileType = (String)sourceFile.get("file_type");
				if(fileType.equalsIgnoreCase("PNG")|| fileType.equalsIgnoreCase("JPG")||fileType.equalsIgnoreCase("JPEG") || fileType.equalsIgnoreCase("TIF")|| fileType.equalsIgnoreCase("TIFF")){

					String imagePath = imageBasicPath + GuidUtil.getLocalPath((String)sourceFile.get("file_hash"))+"\\"
		 					+(String)sourceFile.get("file_name")+"."+fileType;
		        	/*String pageTypeCode = (String)(String)sourceFile.get("mr_page_type_code");
		        	String pageTypeName = (String)(String)sourceFile.get("page_type_name");
		        	if(pageTypeCode == null){
		        		pageTypeCode = "-01";
		        		pageTypeName = "未编页";
		        	}*/

					Image image = Image.getInstance(imagePath);
			        // 获取操作的页面
			        // 根据域的大小缩放图片
			        //image.scaleToFit(signRect.getWidth(), signRect.getHeight());
			        // 添加图片
					float imageHeight=image.getScaledHeight();
					float imageWidth=image.getScaledWidth();
					int i=0;
					while(imageHeight>PageSize.A4.getHeight()||imageWidth>PageSize.A4.getWidth()){
						image.scalePercent(100-i);
						i++;
						imageHeight=image.getScaledHeight();
						imageWidth=image.getScaledWidth();
					}
					/*image.scalePercent(100);*/
					image.setAlignment(Image.ALIGN_CENTER);
					document.newPage();
					/*if(!pageTypes.containsKey(pageTypeCode)){
						document.add(new Chunk(pageTypeName).setLocalDestination(pageTypeCode));
						pageTypes.put(pageTypeCode, pageTypeName);
						pageTypeCodes.add(pageTypeCode);
					}*/

					document.add(image);
				}
				/*Image image = Image.getInstance(BarCodeUtil.generate("123456789"));*/


			}
			/*PdfContentByte cb = writer.getDirectContent();
			PdfOutline root = cb.getRootOutline();
			for (String pageTypeCode : pageTypeCodes) {
				 @SuppressWarnings("unused")
			     PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage(pageTypeCode, false),pageTypes.get(pageTypeCode));

			}*/


		} catch (DocumentException de) {
			de.printStackTrace();
			throw de;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw ioe;
		}finally{
			try {
				if(out!= null){
					out.flush();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			try{
				if(document != null){
					document.close();
				}
			}catch (Exception e) {
				// TODO: handle exception
			}
			try {
				if(out!= null){
					out.close();
				}
			} catch (Exception e) {
				// TODO: handle exception

			}
		}
    }

	/**
	 * 依据模板生成pdf流
	 * @param data
	 * @param pageIndexTemplatePDFPath
	 * @param medicalWorkerMap
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	@SuppressWarnings("unchecked")
	public static ByteArrayOutputStream getPDFStreamByTemplate(Map<String, Object> data,
			String pageIndexTemplatePDFPath, Map<String, Object> signatureMedicalWorks)
			throws IOException, DocumentException {

		// // 模板路径
		// String templatePath = "F:/Publish/medical_record_template.pdf";
		//
		PdfReader reader = null;
		ByteArrayOutputStream bos = null;
		PdfStamper stamper = null;
		try {


			reader = new PdfReader(pageIndexTemplatePDFPath);// 读取pdf模板
			bos = new ByteArrayOutputStream();
			stamper = new PdfStamper(reader, bos);
			AcroFields form = stamper.getAcroFields();

			// 使用中文字体
			BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
			fontList.add(bf);

			form.setSubstitutionFonts(fontList);
			Map<String, Object> basicInfo = (Map<String, Object>) data.get("basicInfo");
			Map<String, Object> cureInfo = (Map<String, Object>) data.get("cureInfo");
			Map<String, Object> diseaseDiagInfo = (Map<String, Object>) data.get("hqmsDiseaseDiagInfo");
			Map<String, Object> operateInfo = (Map<String, Object>) data.get("hqmsOperateInfo");
			Map<String, Object> transferDeptInfo = (Map<String, Object>) data.get("transferDeptInfo");
			Map<String, Object> nurseInfo = (Map<String, Object>) data.get("nurseInfo");
			Map<String, Object> costInfo = (Map<String, Object>) data.get("costInfo");
			System.out.println(costInfo);
			form.setField("fill_2", "49417303-1");
			form.setField("fill_1", "曲阜市人民医院");
			if (basicInfo != null) {

				String patientName = (String) basicInfo.get("patientName");
				if (patientName != null) {
					form.setField("patientName", patientName);

				}

				String medicalPayTypeCode = (String) basicInfo.get("medicalPayTypeCode");
				if (medicalPayTypeCode != null) {
					form.setField("medicalPayTypeCode", medicalPayTypeCode);

				}

				String medicalHealthNumber = (String) basicInfo.get("medicalHealthNumber");
				if (medicalHealthNumber != null) {
					form.setField("medicalHealthNumber", medicalHealthNumber);
				}

				Object visitNumber = (Object) basicInfo.get("visitNumber");
				if (visitNumber != null) {
					form.setField("visitNumber", visitNumber.toString());
				}

				String mrId = (String) basicInfo.get("mrId");
				if (mrId != null) {
					form.setField("mrId", mrId);
				}

				String birth = (String) basicInfo.get("birthday");
				if (birth != null) {
					String birthYear = birth.substring(0, 4);
					String birthMonth =birth.substring(5, 7);
					if(birthMonth.substring(0, 1)=="0") {
						birthMonth = birthMonth.substring(1, 2);
					}
					String birthDay	= birth.substring(8, 10);
					form.setField("birthYear", birthYear);
					form.setField("birthMonth", birthMonth);
					form.setField("birthday", birthDay);
				}

				String sexName = (String) basicInfo.get("sexName");
				if (sexName != null) {
					form.setField("sex", sexName);
				}

				Object yearOfAge = (Object) basicInfo.get("yearOfAge");
				if (yearOfAge != null) {
					form.setField("yearOfAge", yearOfAge.toString());
				}

				String nationalityName = (String) basicInfo.get("nationalityName");
				if (nationalityName != null) {
					form.setField("nationalityName", nationalityName);
				}

				Integer monthOfAge = ObjectUtils.getIntegerValue(basicInfo.get("monthOfAge"));
				if (monthOfAge != null) {
					form.setField("monthOfAge", monthOfAge+"");
				}

				String birthAddress = (String) basicInfo.get("birthAddress");
				if (birthAddress != null) {

					form.setField("birthAddress", birthAddress);
				}

				Map<String, Object> nativePlaceRegionalism = (Map<String, Object>) basicInfo.get("nativePlaceRegionalism");
				String nativePlaceRegionalismProvinceName = (String) nativePlaceRegionalism.get("provinceName");
				if (nativePlaceRegionalismProvinceName != null) {
					form.setField("nativePlaceRegionalismProvinceName", nativePlaceRegionalismProvinceName);
				}
				String nativePlaceRegionalismCityName = (String) nativePlaceRegionalism.get("cityName");
				if (nativePlaceRegionalismProvinceName != null) {
					form.setField("nativePlaceRegionalismCityName", nativePlaceRegionalismCityName);
				}


				String nationName = (String) basicInfo.get("nationName");
				if (nationName != null) {
					form.setField("nationName", nationName);
				}

				String documentTypeName =(String) basicInfo.get("documentTypeName");
				if (documentTypeName != null) {
					form.setField("documentTypeName", documentTypeName);
				}

				String idNumber = (String) basicInfo.get("idNumber");
				if (idNumber != null) {
					form.setField("idNumber", idNumber);
				}

				String professionName = (String) basicInfo.get("professionName");
				if (professionName != null) {
					form.setField("professionName", professionName);
				}

				String marriageName = (String) basicInfo.get("marriageName");
				if (marriageName != null) {
					form.setField("marriageName", marriageName);
				}

				Map<String, Object> birthRegionalism = (Map<String, Object>) basicInfo.get("birthRegionalism");
				if (birthRegionalism != null) {
					String birthRegionalismProvinceName = (String) birthRegionalism.get("provinceName");
					if (birthRegionalismProvinceName != null) {
						form.setField("birthRegionalismProvinceName", birthRegionalismProvinceName);
					}
					String birthRegionalismCityName = (String) birthRegionalism.get("cityName");
					if (birthRegionalismCityName != null) {
						form.setField("birthRegionalismCityName", birthRegionalismCityName);
					}
					String birthRegionalismCountyName = (String) birthRegionalism.get("countyName");
					if (birthRegionalismCountyName != null) {
						form.setField("birthRegionalismCountyName", birthRegionalismCountyName);
					}
					String birthRegionalismStreetName = (String) birthRegionalism.get("extraAddressDesc");
					if (birthRegionalismStreetName != null) {
						form.setField("birthRegionalismStreetName", birthRegionalismStreetName);
					}
				}


				Map<String, Object> permanentAddressRegionalism = (Map<String, Object>) basicInfo.get("permanentAddressRegionalism");
				String permanentAddressRegionalismProvinceName = (String) permanentAddressRegionalism.get("provinceName");
				if (permanentAddressRegionalismProvinceName != null) {
					form.setField("permanentAddressRegionalismProvinceName", permanentAddressRegionalismProvinceName);
				}
				String permanentAddressRegionalismCityName = (String) permanentAddressRegionalism.get("cityName");
				if (permanentAddressRegionalismCityName != null) {
					form.setField("permanentAddressRegionalismCityName", permanentAddressRegionalismCityName);
				}
				String permanentAddressRegionalismCountyName = (String) permanentAddressRegionalism.get("countyName");
				if (permanentAddressRegionalismCountyName != null) {
					form.setField("permanentAddressRegionalismCountyName", permanentAddressRegionalismCountyName);
				}
				String permanentAddressRegionalismStreetName = (String) permanentAddressRegionalism.get("extraAddressDesc");
				if (permanentAddressRegionalismStreetName != null) {
					form.setField("permanentAddressRegionalismStreetName", permanentAddressRegionalismStreetName);
				}

				String permanentAddressMobilePhone = (String) basicInfo.get("permanentAddressMobilePhone");
				if (permanentAddressMobilePhone != null) {
					form.setField("permanentAddressMobilePhone", permanentAddressMobilePhone);
				}

				String permanentAddressPostCode = (String) basicInfo.get("permanentAddressPostCode");
				if (permanentAddressPostCode != null) {
					form.setField("permanentAddressPostCode", permanentAddressPostCode);
				}

				// 鎴峰彛鍦板潃
				Map<String, Object> registeredAddressRegionalism = (Map<String, Object>) basicInfo.get("registeredAddressRegionalism");
				String registeredAddressRegionalismProvinceName = (String) registeredAddressRegionalism.get("provinceName");
				if (registeredAddressRegionalismProvinceName != null) {
					form.setField("registeredAddressRegionalismProvinceName", registeredAddressRegionalismProvinceName);
				}
				String registeredAddressRegionalismCityName = (String) registeredAddressRegionalism.get("cityName");
				if (registeredAddressRegionalismCityName != null) {
					form.setField("registeredAddressRegionalismCityName", registeredAddressRegionalismCityName);
				}
				String registeredAddressRegionalismCountyName = (String) registeredAddressRegionalism.get("countyName");
				if (registeredAddressRegionalismCountyName != null) {
					form.setField("registeredAddressRegionalismCountyName", registeredAddressRegionalismCountyName);
				}
				String registeredAddressRegionalismStreetName = (String) registeredAddressRegionalism.get("extraAddressDesc");
				if (registeredAddressRegionalismStreetName != null) {
					form.setField("registeredAddressRegionalismStreetName", registeredAddressRegionalismStreetName);
				}

				// 鎴峰彛閭紪
				String registeredAddressPostCode = (String) basicInfo.get("registeredAddressPostCode");
				if (registeredAddressPostCode != null) {
					form.setField("registeredAddressPostCode", registeredAddressPostCode);
				}

				// 宸ヤ綔鍗曚綅鍦板潃workUnitRegionalismProvinceName
				Map<String, Object> workUnitRegionalism = (Map<String, Object>) basicInfo.get("workUnitRegionalism");
				String workUnitRegionalismProvinceName = (String) workUnitRegionalism.get("provinceName");

				if (workUnitRegionalismProvinceName != null) {
					form.setField("workUnitRegionalismProvinceName", workUnitRegionalismProvinceName);
				}
				String workUnitRegionalismCityName = (String) workUnitRegionalism.get("cityName");
				if (workUnitRegionalismCityName != null) {
					form.setField("workUnitRegionalismCityName", workUnitRegionalismCityName);
				}
				String workUnitRegionalismCountyName = (String) workUnitRegionalism.get("countyName");
				if (workUnitRegionalismCountyName != null) {
					form.setField("workUnitRegionalismCountyName", workUnitRegionalismCountyName);
				}
				String workUnitRegionalismStreetName = (String) workUnitRegionalism.get("extraAddressDesc");
				if (workUnitRegionalismStreetName != null) {
					form.setField("workUnitRegionalismStreetName", workUnitRegionalismStreetName);
				}

				String workUnitPhone = (String) basicInfo.get("workUnitPhone");
				if (workUnitPhone != null) {
					form.setField("workUnitPhone", workUnitPhone);
				}

				String workUnitPostCode = (String) basicInfo.get("workUnitPostCode");
				if (workUnitPostCode != null) {
					form.setField("workUnitPostCode", workUnitPostCode);
				}

				String linkManName = (String) basicInfo.get("linkManName");
				if (linkManName != null) {
					form.setField("linkManName", linkManName);
				}

				String linkManRelativeRelationName = (String) basicInfo.get("linkManRelativeRelationName");
				if (linkManRelativeRelationName != null) {
					form.setField("linkManRelativeRelationName", linkManRelativeRelationName);
				}
				Map<String, Object> linkManAddressRegionalism = (Map<String, Object>)  basicInfo.get("linkManAddressRegionalism");
				String linkManAddressRegionalismProvince = (String) linkManAddressRegionalism.get("provinceName");
				if (linkManAddressRegionalismProvince != null) {
					form.setField("linkManAddressRegionalismProvince", linkManAddressRegionalismProvince);
				}
				String linkManAddressRegionalismCity = (String) linkManAddressRegionalism.get("cityName");
				if (linkManAddressRegionalismCity != null) {
					form.setField("linkManAddressRegionalismCity", linkManAddressRegionalismCity);
				}
				String linkManAddressRegionalismCounty = (String) linkManAddressRegionalism.get("countyName");
				if (linkManAddressRegionalismCounty != null) {
					form.setField("linkManAddressRegionalismCounty", linkManAddressRegionalismCounty);
				}
				String linkManAddressRegionalismStreet = (String) linkManAddressRegionalism.get("extraAddressDesc");
				if (linkManAddressRegionalismStreet != null) {
					form.setField("linkManAddressRegionalismStreet", linkManAddressRegionalismStreet);
				}

				String linkManPhone = (String) basicInfo.get("linkManPhone");
				if (linkManPhone != null) {
					form.setField("linkManPhone", linkManPhone);
				}

				String inHospitalTypeCode = (String) basicInfo.get("inHospitalTypeCode");
				//System.out.println(inHospitalTypeCode);
				if (inHospitalTypeCode != null) {
					if(inHospitalTypeCode.equalsIgnoreCase("1")) {
						form.setField("inHospitalTypeCode","2");
					}else if(inHospitalTypeCode.equalsIgnoreCase("2")) {
						form.setField("inHospitalTypeCode","1");
					}else {
						form.setField("inHospitalTypeCode", inHospitalTypeCode);
					}

				}

				String shiftToUnitName = (String) basicInfo.get("shiftToUnitName");
				if (shiftToUnitName != null) {
					form.setField("shiftToUnitName", shiftToUnitName);
				}

				String inHospitalDateTime = (String) basicInfo.get("inHospitalDateTime");
				String inHospitalYear = inHospitalDateTime.substring(0, 4);
				String inHospitalMonth = inHospitalDateTime.substring(5, 7);
				String inHospitalDay = inHospitalDateTime.substring(8, 10);
				String inHospitalHour = inHospitalDateTime.substring(11, 13);

				if (inHospitalDateTime != null) {
					if (inHospitalYear != null) {
						form.setField("inHospitalYear", inHospitalYear);
					}
					if (inHospitalMonth != null) {
						form.setField("inHospitalMonth", inHospitalMonth);
					}
					if (inHospitalDay != null) {
						form.setField("inHospitalDay", inHospitalDay);
					}
					if (inHospitalHour != null) {
						form.setField("inHospitalHour", inHospitalHour);
					}
				}

				String inDeptName = (String) basicInfo.get("inDeptName");
				if (inDeptName != null) {
					form.setField("inDeptName", inDeptName);
				}

				String inHospitalHouseName = (String) basicInfo.get("inHospitalHouseName");
				if (inHospitalHouseName != null) {
					form.setField("inHospitalHouseName", inHospitalHouseName);
				}

				String changeDeptDesc = (String) basicInfo.get("changeDeptDesc");
				if (changeDeptDesc != null) {
					form.setField("changeDeptOut", inDeptName);
					form.setField("changeDeptEnter", changeDeptDesc);
				}

				String outHospitalDateTime = (String) basicInfo.get("outHospitalDateTime");
				String outHospitalYear = outHospitalDateTime.substring(0, 4);
				String outHospitalMonth = outHospitalDateTime.substring(5, 7);
				String outHospitalDay = outHospitalDateTime.substring(8, 10);
				String outHospitalHour = outHospitalDateTime.substring(11, 13);
				if (outHospitalDateTime != null) {
					if (outHospitalYear != null) {
						form.setField("outHospitalYear", outHospitalYear);
					}
					if (outHospitalMonth != null) {
						form.setField("outHospitalMonth", outHospitalMonth);
					}
					if (outHospitalYear != null) {
						form.setField("outHospitalDay", outHospitalDay);
					}
					if (outHospitalYear != null) {
						form.setField("outHospitalHour", outHospitalHour);
					}
				}

				String outDeptName = (String) basicInfo.get("outDeptName");
				if (outDeptName != null) {
					form.setField("outDeptName", outDeptName);
				}

				// 鍑洪櫌鐥呮埧
				String outHospitalHouseName = (String) basicInfo.get("outHospitalHouseName");
				if (outHospitalHouseName != null) {
					form.setField("outHospitalHouseName", outHospitalHouseName);
				}

				Object inHospitalDayNumber = (Object) basicInfo.get("inHospitalDayNumber");
				if (inHospitalDayNumber != null) {
					form.setField("inHospitalDayNumber", inHospitalDayNumber.toString());
				}

				String outHospitalTypeCode = (String) basicInfo.get("outHospitalTypeCode");
				if (outHospitalTypeCode != null) {
					form.setField("outHospitalTypeCode", outHospitalTypeCode);
				}

				String receiveUnitName = (String) basicInfo.get("receiveUnitName");
				if (receiveUnitName != null) {
					if (outHospitalTypeCode.equals("2")) {
						form.setField("receiveUnitName", receiveUnitName);
					} else if (outHospitalTypeCode.equals("3")) {
						form.setField("receiveUnitName1", receiveUnitName);
					}

				}



				String whetherDrugAllergyCode = (String) basicInfo.get("whetherDrugAllergyCode");
				if (whetherDrugAllergyCode != null) {

					form.setField("whetherDrugAllergyCode", whetherDrugAllergyCode);
				} else {
					form.setField("whetherDrugAllergyCode", "1");
				}

				String drugAllergyDesc = (String) basicInfo.get("drugAllergyDesc");
				if (drugAllergyDesc != null) {
					form.setField("drugAllergyDesc", drugAllergyDesc);
				}

				String autopsyCode = (String) basicInfo.get("autopsyCode");
				if (autopsyCode != null) {
					form.setField("autopsyCode", autopsyCode);
				}

			}

			if (cureInfo != null) {


				// 鏂扮敓鍎垮嚭鐢熶綋閲嶏紙g锛�
				//System.out.println(basicInfo);
				Object babyBirthWeight = (Object) cureInfo.get("babyBirthWeight");

				if (babyBirthWeight != null) {
					form.setField("babyBirthWeight", babyBirthWeight.toString());

				}

				// 鏂扮敓鍎垮叆闄綋閲嶏紙g锛�
				Object babyInHospitalWeight = (Object) cureInfo.get("babyInHospitalWeight");

				if (babyInHospitalWeight != null) {
					form.setField("babyInHospitalWeight", babyInHospitalWeight.toString());

				}

				// ABO琛�鍨嬬紪鍙�
				String bloodTypeCode = (String) cureInfo.get("bloodTypeCode");
				//System.out.println(bloodTypeCode);
				if (bloodTypeCode != null) {
					form.setField("bloodTypeCode", bloodTypeCode);
				}

				// RH琛�鍨嬬紪鍙�
				String rhBloodTypeCode = (String) cureInfo.get("rhBloodTypeCode");
				if (rhBloodTypeCode != null) {
					form.setField("rhBloodTypeCode", rhBloodTypeCode);
				}

				// 璐ㄦ帶鏃ユ湡
				String qualityControlDateTime = (String) cureInfo.get("qualityControlDateTime");


				if (qualityControlDateTime != null) {

					String qualityControlDateYear = qualityControlDateTime.substring(0, 4);
					String qualityControlDateMonth = qualityControlDateTime.substring(5, 7);
					String qualityControlDateDay = qualityControlDateTime.substring(8, 10);

					if (qualityControlDateYear != null) {
						form.setField("qualityControlDateYear", qualityControlDateYear);
					}
					if (qualityControlDateMonth != null) {
						form.setField("qualityControlDateMonth", qualityControlDateMonth);
					}
					if (qualityControlDateDay != null) {
						form.setField("qualityControlDateDay", qualityControlDateDay);
					}
				}

				// 鍑洪櫌31澶╂槸鍚︽湁浣忛櫌璁″垝
				String rehospitalAimOf31Code = (String) cureInfo.get("rehospitalAimOf31Code");
				if (rehospitalAimOf31Code != null) {
					form.setField("rehospitalAimOf31Code", rehospitalAimOf31Code);
				}

				// 鍑洪櫌31澶╀綇闄㈢洰鐨�
				String rehospitalAimOf31Description = (String) cureInfo.get("rehospitalAimOf31Description");
				if (rehospitalAimOf31Description != null) {
					form.setField("rehospitalAimOf31Description", rehospitalAimOf31Description);
				}

				// 棰呰剳鎹熶激鏄忚糠鏃堕棿

				// 鍏ラ櫌鍓�

				Integer dayOfBeforeInHospitalComa = ObjectUtils.getIntegerValue(cureInfo.get("dayOfBeforeInHospitalComa"));
				if (dayOfBeforeInHospitalComa != null) {
					form.setField("dayOfBeforeInHospitalComa", dayOfBeforeInHospitalComa+"");
				}

				Integer hourOfBeforeInHospitalComa = ObjectUtils.getIntegerValue(cureInfo.get("hourOfBeforeInHospitalComa"));
				if (hourOfBeforeInHospitalComa != null) {
					form.setField("hourOfBeforeInHospitalComa", hourOfBeforeInHospitalComa+"");
				}

				Integer minuteOfBeforeInHospitalComa = ObjectUtils.getIntegerValue(cureInfo.get("minuteOfBeforeInHospitalComa"));
				if (minuteOfBeforeInHospitalComa != null) {
					form.setField("minuteOfBeforeInHospitalComa", minuteOfBeforeInHospitalComa+"");
				}

				// 鍏ラ櫌鍚�
				Integer dayOfAfterInHospitalComa = ObjectUtils.getIntegerValue(cureInfo.get("dayOfAfterInHospitalComa"));
				if (dayOfAfterInHospitalComa != null) {
					form.setField("dayOfAfterInHospitalComa", dayOfAfterInHospitalComa+"");
				}

				Integer hourOfAfterInHospitalComa = ObjectUtils.getIntegerValue(cureInfo.get("hourOfAfterInHospitalComa"));
				if (hourOfAfterInHospitalComa != null) {
					form.setField("hourOfAfterInHospitalComa", hourOfAfterInHospitalComa+"");
				}

				Integer minuteOfAfterInHospitalComa = ObjectUtils.getIntegerValue( cureInfo.get("minuteOfAfterInHospitalComa"));
				if (minuteOfAfterInHospitalComa != null) {
					form.setField("minuteOfAfterInHospitalComa", minuteOfAfterInHospitalComa+"");
				}
				// 鎭舵�ц偪鐦ゅ叆闄㈢‘璇婃棩鏈�
				String malignantTumorConfirmedDateTime = (String) cureInfo.get("malignantTumorConfirmedDateTime");
				if (malignantTumorConfirmedDateTime != null) {
					form.setField("malignantTumorConfirmedDateTime", malignantTumorConfirmedDateTime);
				}

				// 鑲跨槫鍒嗙骇
				String tumorGrade = (String) cureInfo.get("tumorGrade");
				if (tumorGrade != null) {
					form.setField("tumorGrade", tumorGrade);
				}

				// 鑲跨槫鍒嗙骇T
				String tumorStagingOfT = (String) cureInfo.get("tumorStagingOfT");
				if (tumorStagingOfT != null) {
					form.setField("tumorStagingOfT", tumorStagingOfT);
				}

				// 鑲跨槫鍒嗙骇N
				String tumorStagingOfN = (String) cureInfo.get("tumorStagingOfN");
				if (tumorStagingOfN != null) {
					form.setField("tumorStagingOfN", tumorStagingOfN);
				}

				// 鑲跨槫鍒嗙骇M
				String tumorStagingOfM = (String) cureInfo.get("tumorStagingOfM");
				if (tumorStagingOfM != null) {
					form.setField("tumorStagingOfM", tumorStagingOfM);
				}

				// 鎭舵�ц偪鐦ゆ渶鍒濇不鐤楁柟寮忕紪鍙�
				String malignantTumorFirstCureTypeCode = (String) cureInfo.get("malignantTumorFirstCureTypeCode");
				if (malignantTumorFirstCureTypeCode != null) {
					form.setField("malignantTumorFirstCureTypeCode", malignantTumorFirstCureTypeCode);
				}

				// 鎭舵�ц偪鐦ゆ渶楂樿瘖鏂緷鎹紪鍙�
				String malignantTumorHighestDiagBasisName = (String) cureInfo.get("malignantTumorHighestDiagBasisName");
				if (malignantTumorHighestDiagBasisName != null) {
					form.setField("malignantTumorHighestDiagBasisName", malignantTumorHighestDiagBasisName);
				}

				// 鐥呮璐ㄩ噺浠ｇ爜
				String medicalRecordQualityCode = (String) cureInfo.get("medicalRecordQualityCode");
				if (medicalRecordQualityCode != null) {
					if (medicalRecordQualityCode != null) {
						form.setField("medicalRecordQualityCode", medicalRecordQualityCode);
					}
				}
				//鍏ュ緞鎯呭喌
				String whetherEnterClinicalPathsCode = (String) cureInfo.get("whetherEnterClinicalPathsCode");
				if (whetherEnterClinicalPathsCode != null) {
					form.setField("whetherEnterClinicalPathsCode", whetherEnterClinicalPathsCode);
				}

				//瀹屾垚鎯呭喌
				String whetherFinishClinicalPathsCode = (String) cureInfo.get("whetherFinishClinicalPathsCode");
				if (whetherFinishClinicalPathsCode != null) {
					form.setField("whetherFinishClinicalPathsCode", whetherFinishClinicalPathsCode);
				}



				//鍙樺紓鎯呭喌
				String whetherConditionVariationCode = (String) cureInfo.get("whetherConditionVariationCode");
				if (whetherConditionVariationCode != null) {
					form.setField("whetherConditionVariationCode", whetherConditionVariationCode);
				}
				// 鍖诲姟浜哄憳
				List<Map<String, Object>> cureWorkers = (List<Map<String, Object>>) cureInfo.get("cureWorkers");
				if (cureWorkers != null) {
					for (Map<String, Object> cureWorker : cureWorkers) {
						String professionTitleCode = (String) cureWorker.get("professionTitleCode");
						String professionTitleName = (String) cureWorker.get("professionTitleName");
						if (professionTitleCode != null) {
							if (professionTitleCode.contains("A1001")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								//String medicalWorkerCode = (String) cureWorker.get("medicalWorkerCode");

								if (medicalWorkerName != null) {
									form.setField("headOfDepartment", medicalWorkerName);
								}

							} else if (professionTitleCode.contains("A1003")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");

								if (medicalWorkerName != null) {
									form.setField("teamLeader", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A2001")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");

								if (medicalWorkerName != null) {
									form.setField("chiefPhysician", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A2003")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("attendingPhysician", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A2005")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("resident", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A3001")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("nurse", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A2006")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("refresherDoctors", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A2008")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("houseman", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A4001")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("coder", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A2009")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("qualityDoctors", medicalWorkerName);
								}
							} else if (professionTitleCode.contains("A3003")) {
								String medicalWorkerName = (String) cureWorker.get("medicalWorkerName");
								if (medicalWorkerName != null) {
									form.setField("qualityNurse", medicalWorkerName);
								}
							}
						}
					}
				}


			}
			if (diseaseDiagInfo != null) {
				Map<String, Object> outpatientDiagRecord = (Map<String, Object>) diseaseDiagInfo.get("outpatientDiagRecord");
				if(outpatientDiagRecord!=null) {
					String diseaseDiagName = (String) outpatientDiagRecord.get("diseaseDiagName");
					String diseaseDiagCode = (String) outpatientDiagRecord.get("diseaseDiagCode");
					if(diseaseDiagName!=null) {
						form.setField("outpatientOriginalDiagShow", diseaseDiagName);
					}
					if(diseaseDiagCode!=null) {
						form.setField("outpatientOriginalDiagCode", diseaseDiagCode);
					}
				}
				//涓昏璇婃柇
				Map<String, Object> mainDiagRecord = (Map<String, Object>) diseaseDiagInfo.get("mainDiagRecord");
				if(mainDiagRecord!=null) {
					String diseaseDiagName = (String) mainDiagRecord.get("diseaseDiagName");
					String diseaseDiagCode = (String) mainDiagRecord.get("diseaseDiagCode");
					String inHospitalDiseaseStateCode = (String) mainDiagRecord.get("inHospitalDiseaseStateCode");
					if(diseaseDiagName!=null) {
						form.setField("mainDiagOriginalDiagShow", diseaseDiagName);
					}
					if(diseaseDiagCode!=null) {
						if(diseaseDiagCode.length() >7){
							form.setField("mainDiagOriginalDiagShowCode", diseaseDiagCode.substring(0, 7));
						}else{
							form.setField("mainDiagOriginalDiagShowCode", diseaseDiagCode);
						}

					}
					if(inHospitalDiseaseStateCode!=null) {
						form.setField("mainDiagInHospitalDiseaseStateShowCode", inHospitalDiseaseStateCode);
					}
				}

				//鍏朵粬璇婃柇

				List<Map<String, Object>> diseaseDiagRecords = (List<Map<String, Object>>) diseaseDiagInfo.get("diseaseDiagRecords");
				if (diseaseDiagRecords != null) {
					int otherDiagIndex = 1;
					for (Map<String, Object> diseaseDiagRecord : diseaseDiagRecords) {
						String diagTypeName = (String) diseaseDiagRecord.get("diagTypeName");
						String diseaseDiagName = (String) diseaseDiagRecord.get("diseaseDiagName");
						String diseaseDiagCode = (String) diseaseDiagRecord.get("diseaseDiagCode");
						String inHospitalDiseaseStateCode = (String) diseaseDiagRecord
								.get("inHospitalDiseaseStateCode");

						if (diseaseDiagName != null) {
							form.setField("ohterDiagOriginalDiagShow" + otherDiagIndex, diseaseDiagName);
						}
						if (diseaseDiagCode != null) {
							if(diseaseDiagCode.length() >7){
								form.setField("ohterDiagOriginalDiagShowCode" + otherDiagIndex, diseaseDiagCode.substring(0, 7));
							}else{
								form.setField("ohterDiagOriginalDiagShowCode" + otherDiagIndex, diseaseDiagCode);
							}

						}
						if (inHospitalDiseaseStateCode != null) {
							form.setField("otherDiagInHospitalDiseaseStateShowCode" + otherDiagIndex,
											inHospitalDiseaseStateCode);
						}
						otherDiagIndex++;
					}
				}

				//鎹熶激涓瘨鐨勫閮ㄥ師鍥�
				List<Map<String, Object>> damageAndVenenationDiagRecords = (List<Map<String, Object>>) diseaseDiagInfo.get("damageAndVenenationDiagRecords");

				if(damageAndVenenationDiagRecords!=null) {
					for (Map<String, Object> damageAndVenenationDiagRecord : damageAndVenenationDiagRecords) {
						String diagTypeName = (String) damageAndVenenationDiagRecord.get("diagTypeName");
						String diseaseDiagName = (String) damageAndVenenationDiagRecord.get("diseaseDiagName");
						String diseaseDiagCode = (String) damageAndVenenationDiagRecord.get("diseaseDiagCode");
						/*System.out.println(diseaseDiagName);
						System.out.println(diagTypeName);*/
						if(diseaseDiagName!=null) {
							form.setField("damageAndVenenationDiagOriginalShow", diseaseDiagName);
						}
						if(diseaseDiagCode!=null) {
							form.setField("damageAndVenenationDiagOriginalShowCode", diseaseDiagCode);
						}
					}
				}

				//鐥呯悊璇婃柇
				List<Map<String, Object>> pathologyDiagRecords = (List<Map<String, Object>>) diseaseDiagInfo.get("pathologyDiagRecords");

				if(pathologyDiagRecords!=null) {
					for (Map<String, Object> pathologyDiagRecord : pathologyDiagRecords) {
						String diagTypeName = (String) pathologyDiagRecord.get("diagTypeName");
						String diseaseDiagName = (String) pathologyDiagRecord.get("diseaseDiagName");
						String diseaseDiagCode = (String) pathologyDiagRecord.get("diseaseDiagCode");
						if(diseaseDiagName!=null) {
							form.setField("pathologyDiagOriginalShow", diseaseDiagName);
						}
						if(diseaseDiagCode!=null) {
							form.setField("pathologyDiagOriginalShowCode", diseaseDiagCode);
						}
						String pathologyNumber = (String) pathologyDiagRecord.get("pathologyNumber");
						if (pathologyNumber != null) {
							form.setField("pathologyNumber", pathologyNumber);
						}
					}
				}
			}

			if(nurseInfo != null){
				Integer specialNurseDayNumberInteger = (Integer) nurseInfo.get("specialNurseDayNumber");
				//String specialNurseDayNumber = specialNurseDayNumberInteger.toString();
				if(specialNurseDayNumberInteger != null) {
					form.setField("specialNurseDayNumber", specialNurseDayNumberInteger.toString());
				}
				//涓�绾ф姢鐞�
				Integer firstLevelNurseDayNumberInteger = (Integer) nurseInfo.get("firstLevelNurseDayNumber");
				if(firstLevelNurseDayNumberInteger != null) {
					form.setField("firstLevelNurseDayNumber", firstLevelNurseDayNumberInteger.toString());
				}

				//浜岀骇鎶ょ悊
				Integer secondLevelNurseDayNumberInteger = (Integer) nurseInfo.get("secondLevelNurseDayNumber");
				if(secondLevelNurseDayNumberInteger != null) {
					form.setField("secondLevelNurseDayNumber", secondLevelNurseDayNumberInteger.toString());
				}

				//涓夌骇鎶ょ悊
				Integer threeLevelNurseDayNumberInteger = (Integer) nurseInfo.get("threeLevelNurseDayNumber");
				if(threeLevelNurseDayNumberInteger != null) {
					form.setField("threeLevelNurseDayNumber", threeLevelNurseDayNumberInteger.toString());
				}
			}

			if (operateInfo != null) {


				// 鎵嬫湳淇℃伅
				List<Map<String, Object>> operateRecords = (List<Map<String, Object>>) operateInfo
						.get("operateRecords");
				if (operateRecords != null) {
					int operateRecordIndex = 1;
					for (Map<String, Object> operateRecord : operateRecords) {
						// 鎵嬫湳缂栫爜
						String operateCode = (String) operateRecord.get("operateCode");
						if (operateCode != null) {
							form.setField("operateDescShow" + operateRecordIndex, operateCode);

						}
						// 鎵嬫湳寮�濮嬫棩鏈�
						String operateStartDateStr = (String) operateRecord.get("operateStartDate");
						String operateStartDate = operateStartDateStr.substring(0, 10);
						if (operateStartDate != null) {
							form.setField("operateStartDate" + operateRecordIndex, operateStartDate);

						}

						// 鎵嬫湳绾у埆缂栧彿
						String opsLevelCode = (String) operateRecord.get("opsLevelCode");
						if (opsLevelCode != null) {
							form.setField("opsLevelCode" + operateRecordIndex, opsLevelCode);

						}

						// 鎵嬫湳鍚嶇О
						String operateName = (String) operateRecord.get("operateName");
						if (operateName != null) {
							form.setField("operateOriginalShow" + operateRecordIndex, operateName);

						}

						// 鎵嬫湳鍒囧彛绛夌骇鍚嶇О
						String incisionLevelName = StringUtil
								.meaningStr((String) operateRecord.get("incisionLevelName"));
						String cicatrizeTypeName = StringUtil
								.meaningStr((String) operateRecord.get("cicatrizeTypeName"));
						if (incisionLevelName != null & cicatrizeTypeName != null) {
							form.setField("incisionLevelName" + operateRecordIndex,
									incisionLevelName.substring(0, 1) + "/" + cicatrizeTypeName);

						}

						// 鎵嬫湳绫诲瀷
						String operatMarkName = (String) operateRecord.get("operatMarkName");
						if (operatMarkName != null) {
							form.setField("operatMarkCode" + operateRecordIndex, operatMarkName);

						}

						// 楹婚唹鏂瑰紡鍚嶇О
						String anaesthesiaTypeName = (String) operateRecord.get("anaesthesiaTypeName");
						if (anaesthesiaTypeName != null) {
							form.setField("anaesthesiaTypeName" + operateRecordIndex, anaesthesiaTypeName);

						}

						// 鎵嬫湳浜哄憳
						List<Map<String, Object>> operateWorkers = (List<Map<String, Object>>) operateRecord
								.get("operateWorkers");
						if (operateWorkers != null) {
							int operateWorkerIndex = 0;

							for (Map<String, Object> operateWorker : operateWorkers) {
								String professionTitleCode = (String) operateWorker.get("professionTitleCode");
								String professionTitleName = (String) operateWorker.get("professionTitleName");
								if (professionTitleCode != null) {
									if (professionTitleCode.contains("A5001")) {
										String medicalWorkerName = (String) operateWorker.get("medicalWorkerName");
										if (medicalWorkerName != null) {
											form.setField("performer" + operateRecordIndex, medicalWorkerName);
										}
									} else if (professionTitleCode.contains("A5003")) {
										String medicalWorkerName = (String) operateWorker.get("medicalWorkerName");
										if (medicalWorkerName != null) {
											form.setField("firstAssisant" + operateRecordIndex, medicalWorkerName);
										}
									} else if (professionTitleCode.contains("A5004")) {
										String medicalWorkerName = (String) operateWorker.get("medicalWorkerName");
										if (medicalWorkerName != null) {
											form.setField("secondAssisant" + operateRecordIndex, medicalWorkerName);
										}
									} else if (professionTitleCode.contains("A5002")) {
										String medicalWorkerName = (String) operateWorker.get("medicalWorkerName");
										if (medicalWorkerName != null) {
											form.setField("anesthesiologists" + operateRecordIndex, medicalWorkerName);
										}
									}
								}

							}
						}
						operateRecordIndex++;
					}

				}

			}

			if(costInfo!=null){
				String totalCost = MapUtils.getString(costInfo, "totalCost");
				if(totalCost!=null){
					form.setField("totalCost", totalCost);
				}
				String selfCost = MapUtils.getString(costInfo, "selfCost");
				if(selfCost!=null){
					form.setField("selfCost", selfCost);
				}

				List<Map<String,Object>> costRecords = (List<Map<String, Object>>) MapUtils.getObject(costInfo, "costRecords");
				if(CollectionUtils.isNotEmpty(costRecords)){
					for (Map<String, Object> costRecord : costRecords) {
						String medicalCostTypeCode = MapUtils.getString(costRecord, "medicalCostTypeCode");
						String costMoney = MapUtils.getString(costRecord, "costMoney");
						if(StringUtils.isNotBlank(medicalCostTypeCode)){
							//一般医疗服务费
							if("C01001".equals(medicalCostTypeCode)){
								form.setField("serviceFee", costMoney);
							//一般治疗操作费
							}else if("C01002".equals(medicalCostTypeCode)){
								form.setField("handlingFee", costMoney);
							//护理费
							}else if("C01003".equals(medicalCostTypeCode)){
								form.setField("nursingFee", costMoney);

							//其他费用
							}else if("C01999".equals(medicalCostTypeCode)){
								form.setField("othersServiceFee", costMoney);
							//2 诊断类
							}else if("C02001".equals(medicalCostTypeCode)){
								//病理诊断费
								form.setField("pathology", costMoney);
							}else if("C02002".equals(medicalCostTypeCode)){
								//实验室诊断费
								form.setField("label", costMoney);
							}else if("C02003".equals(medicalCostTypeCode)){
								//影像学诊断费
								form.setField("image", costMoney);
							}else if("C02004".equals(medicalCostTypeCode)){
								//临床诊断项目费
								form.setField("clinicalDiagnosis", costMoney);
							//3 治疗类
							}else if("C03001".equals(medicalCostTypeCode)){
								//非手术治疗项目费
								form.setField("non-surgical", costMoney);
							}else if("C03002".equals(medicalCostTypeCode)){
								//临床物理治疗费
								form.setField("Physical", costMoney);
							}else if("C03003".equals(medicalCostTypeCode)){
								//手术治疗费
								form.setField("operativeTreatment", costMoney);
							}else if("C03004".equals(medicalCostTypeCode)){
								//麻醉费
								form.setField("anesthesia", costMoney);
							}else if("C03005".equals(medicalCostTypeCode)){
								//手术费
								form.setField("surgical", costMoney);
							//4 康复类
							}else if("C04001".equals(medicalCostTypeCode)){
								//康复费
								form.setField("rehabilitation", costMoney);
							//5 中医类
							}else if("C05001".equals(medicalCostTypeCode)){
								//中医治疗费
								form.setField("TCMTreatmentFee", costMoney);
							//6 西药类
							}else if("C06001".equals(medicalCostTypeCode)){
								//西药费
								form.setField("westernMedicine", costMoney);
							}else if("C06002".equals(medicalCostTypeCode)){
								//抗菌药物费用
								form.setField("antibacterial", costMoney);
							//中药类
							}else if("C07001".equals(medicalCostTypeCode)){
								//中成药费
								form.setField("chinesePatentMedicine", costMoney);
							}else if("C07002".equals(medicalCostTypeCode)){
								//中草药费
								form.setField("chineseHerbalMedicine", costMoney);
							//8 血液和血液制品类
							}else if("C08001".equals(medicalCostTypeCode)){
								//血费
								form.setField("bloodFee", costMoney);
							}else if("C08002".equals(medicalCostTypeCode)){
								//白蛋白类制品费
								form.setField("proteinFee", costMoney);
							}else if("C08003".equals(medicalCostTypeCode)){
								//球蛋白类制品费
								form.setField("globulin", costMoney);
							}else if("C08004".equals(medicalCostTypeCode)){
								//凝血因子类制品费
								form.setField("clottingFactor", costMoney);
							}else if("C08005".equals(medicalCostTypeCode)){
								//细胞因子类制品费
								form.setField("cytokines", costMoney);
							// 9 耗材类
							}else if("C09001".equals(medicalCostTypeCode)){
								//检查用一次性医用材料费
								form.setField("checkOneTime", costMoney);
							}else if("C09002".equals(medicalCostTypeCode)){
								//治疗用一次性医用材料费
								form.setField("OneTreatment", costMoney);
							}else if("C09003".equals(medicalCostTypeCode)){
								//手术用一次性医用材料费
								form.setField("OneOperation", costMoney);
								//10 其他类
							}else if("C99999".equals(medicalCostTypeCode)){
								//其他费用
								form.setField("otherFee", costMoney);
							}

						}


					}
				}
			}




			stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑，一定要设为true
			stamper.close();
			return bos;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} catch (DocumentException e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}

	}

	/**
	 * 依据模板生成pdf
	 * @param data
	 * @param pageIndexTemplatePDFPath
	 * @param newPDFPath
	 * @param newPDFName
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static synchronized void createPDFByTemplate(Map<String, Object> data,String pageIndexTemplatePDFPath,String newPDFPath) throws IOException, DocumentException{

        FileOutputStream out = null;
        ByteArrayOutputStream bos = null;

        try {
        	File newFile = new File(newPDFPath);
			if(!newFile.exists()){
				File parentFile = new File(newFile.getParent());
				if(!parentFile.exists()){
					parentFile.mkdirs();
				}
			}
            out = new FileOutputStream(newPDFPath);// 输出流
            bos = getPDFStreamByTemplate(data, pageIndexTemplatePDFPath, new HashMap<String, Object>());



            Document doc = new Document(PageSize.A4);
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfReader pf =new PdfReader(bos.toByteArray());
            for(int index =1;index<=pf.getNumberOfPages();index++){
            	PdfImportedPage importPage = copy.getImportedPage(pf, index);
            	copy.addAnnotation(new PdfAnnotation(copy, 0, 0, 0, 0,new PdfString( "text"+index), new PdfString( "text"+index)));
                copy.addPage(importPage);
            }
            PdfContentByte cb = copy.getDirectContent();
			PdfOutline root = cb.getRootOutline();
			PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("text1", false),"text1");
			PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("text2", false),"text2");
            doc.close();
        }  catch (IOException e) {
            e.printStackTrace();
            throw e;
        } catch (DocumentException e) {
            e.printStackTrace();
            throw e;
        }finally{
        	if(bos != null){
        		try {
        			bos.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
        	}
        }

    }


	public static ByteArrayOutputStream getPDFStream(String pageIndexPDFPath,String imagePDFPath) throws DocumentException, IOException{
		Document document = new Document();
    	document.setPageSize(PageSize.A4);
		ByteArrayOutputStream bos = null;
		try {
			PdfReader pdfReader = new PdfReader(pageIndexPDFPath);
			bos = new ByteArrayOutputStream();
			PdfWriter writer = PdfWriter.getInstance(document, bos);
			// 打开文档
			document.open();
			PdfContentByte cb = writer.getDirectContent();
			int pageOfCurrentReaderPDF = 0;
			while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
    	        document.newPage();
    	        pageOfCurrentReaderPDF++;
                PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);

    	    }
			PdfReader pdfReader1= new PdfReader(imagePDFPath);

			int pageOfCurrentReaderPDF1 = 0;
			while (pageOfCurrentReaderPDF1 < pdfReader1.getNumberOfPages()) {
    	        document.newPage();

    	        pageOfCurrentReaderPDF1++;
                PdfImportedPage page = writer.getImportedPage(pdfReader1, pageOfCurrentReaderPDF1);

                cb.addTemplate(page, 0, 0);

    	    }
		} catch (DocumentException de) {
			de.printStackTrace();
			throw de;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw ioe;
		}finally{

			try{
				if(document != null){
					document.close();
				}
			}catch (Exception e) {
				// TODO: handle exception
			}

		}
		return bos;
    }
}
